package com.macro.mall.portal.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;

import java.util.UUID;


public class AliPay {

    public class AlipayConfig {
        // 商户appid
        public static String APPID = "9021000141606289";
        // 私钥 pkcs8格式的
        public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCTndbgS9XQIEG34Cm/CgJJB2BpCg2NStJH+cR5V7v6X3lyQZyulFPA0Wk75jkGwjqCnB9fJz94AhbSb8F6lV/1JI0MB7gVHuj9qoeSFLdbGt2TOoaZzdTRBw2+0cRLvfCiBL2Zj3lVCKEfutoX1VsXKmJan/xSE030+bOQXiJf/65AjYDTOFCh2CqdqDEBh5YFDZn1gk5st6ZG/D4+Pi5IxjSsuTw5OzDQVjczFZ+ftP+/+CHK3rxKPosEZRrsV4Rys8g2R0vLmTkpcH8XZdx21ZdQoY+2xhkIDnxRjnru95/TjDZP08D0qRmb0BYPf2EfqEZaLwpJksTp69teX5g7AgMBAAECggEAbNtj3wnthcqTzA4xmuC9qK8Y6LzyG3TGty84NSd+xvbrL1e1PITDcpi7GuH+wb4vuat58wiVZGFeBRdGvag0GCo5HVr/5AtkU9QSn6ZJRJT+OxlYs0k3nGGwQDyeniBWuJP4YaqNFikD8MsxE4xR3TZzS1NZlgkIXxRY16ijftiGH7ZcvEVMglZkI696I/U/QU+KZr2z0Km06+yX8eYcSpsPQjqsJy7iGAgm0WXaWFgQ366WFed7zGsgqTF8DvhZlouEJ2UaEZ7FIVeTvpH9qMEtoE9g0inunLYr1Or7rycxYRskyaZh9tlCL7y5eRGv9auGjIgFD4uU8vaA4WGJuQKBgQDFbbCiWraC8QzmwLWIoL1O0GgBwUl8N5ePuUCrwQo33uwLnd+TqHnonj7UR0mtjJak3RUOa8inBp2Wj1DT589ZtQgTikQL38LlzjBnBEHxHZMsJaW/T2xQbg8Ekvuix8sgWNSTl0DxqLuqSVzTXTlnXwxLzibAc3cGoFgpdF/NhQKBgQC/aQfpCPI5M3N0KTS2aw8cpB5aNh4kcSHxuw0kTu2IQ2jpIcg8CRJMbwmi2RWBu/r1iiNgpFC9FciKBxy22JtW2DFALMV+l4Jn+M0nRWpxVptDfZc7ws98FoAZDech+3VGuxFxJa+fUJ06Inhhs3LMg42eB2GX2RT3PlLa2vravwKBgQCwX9IQ+OFcEWTpMPf8Wz1ED8Ma7kZsBm4MI/+2cPpj928A4cU+8KAYRGb01crolFRwbGHHRFs5fBuEyfv3k3wWNDhG3Jr10K0ZCB8NqSwP78/u/KMaSiYFpI0eFQFg5wR8nV8ri1Clk4w0EN4wpXQG+U7O2hicjYqkDNM2FZUQ0QKBgCPly9D9l4f4yUdYzwUDMRNbCEI88gw9l3MhdIaBsvSiR9OpPUddICxrirAa7HinS5qvLJFcX7QGmNSiHtV/7CouY5Y4j1V9jrx3IeqijyLwbjALJsjTcVxBUAbi35007rq0LMI+oiBL2yDjBBa2s+n9JhskIT58PP1dl+ZpuTA7AoGAC8MHb+B55mC6znjF6Pj+to6yXb36Mq13b8tmQCexHiWQfrsEVsAQ5fO2X8HMKk4AKEOlRM0iocELEZWWbxyX4JxPu7d40hE34QaYZOAEziwenCffpH4cHv7417Fh1a29wO2CPmjG9ocjg/UciHFTI8Fhjh1lP1tnV88HSTS45WU=";
        // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
        public static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
        // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
        public static String return_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/return_url.jsp";
        // 请求网关地址
        public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        // 编码
        public static String CHARSET = "UTF-8";
        // 返回格式
        public static String FORMAT = "json";
        // 支付宝公钥
        public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8LXl6zM9lpBjaPV2eiAJhJPQmf+EvwWAWLYCSKRneEvip1DnHDvMaao9nmiAl0t6EVqxO4tUNQlWcfn5pnI+q4/d7iGeCtYfY08cM0W6asiKlHvXGiS1ndLkjyR/dk5Yul7ZgOPg2DfE1eH6JQt48LFGjMH/6RNmLxTcb/AdEwRvH38KSKUPqQHI/7UJNW+G3SaomWkn/ogXa4rCXQaqjX9Q2An5gpS9Mu104LUPMEj+8lCWisK1jAJYO6P82RJHAtVr1Y16o01eC37KVGutGm3KOmDwgHOAYVsgt9UxC7gST2Q1xqGY/0Rbc2XB0hNawthZ+6fr1j0Q8wMWf3IWSwIDAQAB";
        // RSA2
        public static String SIGNTYPE = "RSA2";
    }


    public static void main( ) {

        final String orderId = UUID.randomUUID().toString().replaceAll("-", "");
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderId ;
        // 订单名称，必填
        String subject = "苹果16Pro";
        System.out.println(subject);
        // 付款金额，必填
        String total_amount = "10000";
        // 商品描述，可空
        String body = "非常拉胯的菠萝手机";
        // 超时时间 可空
        String timeout_express="2m";
        // 销售产品码 必填
        String product_code="jfefsf";
        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(AlipayConfig.notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(AlipayConfig.return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            System.out.println(form);
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
